//
//  WWTQLocation.m
//  WWTQ
//
//  Created by JP on 2018/11/5.
//  Copyright © 2018年 Jqert. All rights reserved.
//

#import "WWTQLocation.h"

@interface WWTQLocation () <CLLocationManagerDelegate>

@property (nonatomic, strong) CLLocationManager *locationManager;
@end

@implementation WWTQLocation

#pragma mark - public
- (void)beginUpdatingLocation {
    //定位开启判定
    if([CLLocationManager locationServicesEnabled] && [CLLocationManager authorizationStatus] == kCLAuthorizationStatusDenied){
        if (self.delegate && [self.delegate respondsToSelector:@selector(sentErrorLocation)]) {
            [self.delegate sentErrorLocation];
        }
    } else {
       [self.locationManager startUpdatingLocation];
    }
}

#pragma mark - location delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    CLLocation * newLocation = locations.lastObject;
    Location * location = [[Location alloc] init];
    location.longitude = newLocation.coordinate.longitude;
    location.latitude = newLocation.coordinate.latitude;
    //根据经纬度反向地理编译出地址信息
    CLGeocoder * geocoder = [[CLGeocoder alloc] init];
    [geocoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
        if (placemarks.count > 0) {
            CLPlacemark * placemark = placemarks.firstObject;
            //存储位置信息
            location.country = placemark.country;
            location.administrativeArea = placemark.administrativeArea;
            location.locality = placemark.locality;
            location.subLocality = placemark.subLocality;
            location.thoroughfare = placemark.thoroughfare;
            location.subThoroughfare = placemark.subThoroughfare;
            
            //设置代理方法
            if ([self.delegate respondsToSelector:@selector(locationDidEndUpdatingLocation:)]) {
                [self.delegate locationDidEndUpdatingLocation:location];
            }
            // 直接文字
            [SVProgressHUD showImage:[UIImage imageNamed:@"dingwei"] status:@"定位成功"];
        } else {
            if ([[self getInternetStatus] isEqualToString:@"notReachable"]) {
                buildErrorSVP(@"当前没有网络，请联网后再试");
            } else {
                buildErrorSVP(@"定位失败,请手动选择");
            }
        }
    }];
    [manager stopUpdatingLocation];
}

#pragma mark - setter and getter
- (CLLocationManager *)locationManager {
    if (!_locationManager) {
        _locationManager = [[CLLocationManager alloc] init];
        _locationManager.delegate = self;
        _locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        _locationManager.distanceFilter = kCLDistanceFilterNone;
        // 取得定位权限，有两个方法，取决于你的定位使用情况
        // 一个是 requestAlwaysAuthorization
        // 一个是 requestWhenInUseAuthorization
        [_locationManager requestAlwaysAuthorization];//ios8以上版本使用。
        [_locationManager requestWhenInUseAuthorization];
    }
    return _locationManager;
}

#pragma mark - 获取网络状态
- (NSString *)getInternetStatus {
    Reachability *reachability  = [Reachability reachabilityWithHostName:@"www.baidu.com"];
    NetworkStatus internetStatus = [reachability currentReachabilityStatus];
    NSString *net = @"wifi";
    switch (internetStatus) {
        case ReachableViaWiFi:
            net = @"wifi";
            break;
        case ReachableVia2G:
            net = @"2G";
            break;
        case ReachableVia3G:
            net = @"3G";
            break;
        case ReachableVia4G:
            net = @"4G";
            break;
            
        case NotReachable:
            net = @"notReachable";
        default:
            break;
    }
    return net;
}

@end
